<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/6/3
 * Time: 10:46
 */

namespace app\index\controller;


use app\common\exception\CustomerException;

class House extends Base
{

    public function initialize()
    {
        $this->allow_action = ["index", "searchHouseByMap", "detail"];
        parent::initialize();
    }

    /**
     * 房屋列表
     * @return array
     */
    public function index()
    {
        $requestData = $this->request->get();
        return (new \app\common\model\House())->getIndexData($requestData);
    }

    /**
     * 根据地理位置查询周边房屋
     * @return mixed
     * @throws CustomerException
     */
    public function searchHouseByMap()
    {
        $requestData = $this->request->get();
        if (empty($requestData['lng'])) throw new CustomerException("缺少参数经度");
        if (empty($requestData['lat'])) throw new CustomerException("缺少参数纬度");
        if (empty($requestData['distance'])) throw new CustomerException("缺少参数距离");
        $requestData = $this->request->get();
        return (new \app\common\model\House())->searchHouseByMap($requestData['lng'], $requestData['lat'], $requestData['distance']);
    }

    /**
     * 房屋详情
     * @return array
     * @throws CustomerException
     */
    public function detail()
    {
        $requestData = $this->request->get();
        if (empty($requestData['id'])) throw new CustomerException("缺少参数房源id");
        $houseModel = new \app\common\model\House();
        $findData = $houseModel->selectById($requestData['id'], true);
        $findData['nearbyHouseList'] = $houseModel->searchHouseByMap($findData['longitude'], $findData['latitude'], 1000);
        return $findData;
    }


}